<?php
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class InvitecodeController extends ControllerBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function listAction(){
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 100;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $invite = new Invitecode();
        $list = $invite->invite_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $list,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $this->ok($list_items);
    }



    public function addinviteAction(){
        $this->validata_filed($this->params);
        $detail['title'] = $this->params['title'];
        $detail['describe'] = $this->params['describe'];
        $detail['fileurl'] = $this->params['fileurl'];
        $detail['url'] = $this->params['url'];
        $detail['word'] = $this->params['word'];
        $detail['published'] = 1;
        $detail['created_at'] = date('Y-m-d H:i:s',time());
        $detail['updated_at'] = date('Y-m-d H:i:s',time());
        $invite = new Invitecode();
        $result = $invite->invite_save($detail);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }
    }


    public function updateinviteAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'id不能为空');
        }
        $this->validata_filed($this->params);
        $invite = new Invitecode();
        $detail = $invite->invite_detail($this->params['id']);
        if(empty($detail)){
            $this->fail(-1,'该公告不存在或以被删除');
        }
        $detail['title'] = $this->params['title'];
        $detail['describe'] = $this->params['describe'];
        $detail['fileurl'] = $this->params['fileurl'];
        $detail['url'] = $this->params['url'];
        $detail['word'] = $this->params['word'];
        $detail['published'] = 1;
        $detail['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $invite->invite_save($detail);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }
    }


    private function validata_filed($params){
        if(empty($params['fileurl'])){
            $this->fail(-1,'图片地址不能为空');
        }
    }


    public function delinviteAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'id不能为空');
        }
        $invite = new Invitecode();
        $detail = $invite->findFirstById($this->params['id']);
        if(empty($detail)){
            $this->fail(-1,'不存在，或已被删除');
        }
        $result = $detail->delete();
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'删除失败');
        }
    }

}
